# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/3/4
# @File   : EC08_UnpaidCreditAndLoanCnt.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:未结清信贷笔数
code:EC08_UnpaidCreditAndLoanCnt
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ
type:常用指标
dataSourceType:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def EC08_UnpaidCreditAndLoanCnt():
    try:
        ECRSummaryUnsettled02 = getTable('ECRSummaryUnsettled02')
        if ECRSummaryUnsettled02 is None:
            return u'缺失值'
        elif ECRSummaryUnsettled02 == [{}]:
            return 0
        else:
            flag = []
            for i in ECRSummaryUnsettled02:
                count_flag = 0
                if i.get('LoanType') == u'合计' and i.get('ReportNo') not in null_type_list:
                    normal_num = i.get('Normalsum_Number')
                    concerned_num = i.get('Concernedsum_Number')
                    bad_num = i.get('Badsum_Number')

                    try:
                        normal_num = float(normal_num)
                    except:
                        normal_num = 0
                        count_flag += 1
                    try:
                        concerned_num = float(concerned_num)
                    except:
                        concerned_num = 0
                        count_flag += 1
                    try:
                        bad_num = float(bad_num)
                    except:
                        bad_num = 0
                        count_flag += 1

                    if count_flag == 3:
                        return u'缺失值'
                    else:
                        return normal_num + concerned_num + bad_num
                else:
                    flag.append(None)
            if len(flag) == len(ECRSummaryUnsettled02):
                return u'缺失值'

    except:
        return u'缺失值'

result = EC08_UnpaidCreditAndLoanCnt()